package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.service.IMyCollectionService;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("myCollections")
public class MyCollectionController {

    @Autowired
    private IMyCollectionService myCollectionService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private INewsService newsService;

    /**
     * 点击收藏游记
     * 已存在收藏点击则取消收藏
     */
    @PostMapping
    public JsonResult collectTravel(Long travelId,Long userId){
        JsonResult jsonResult = new JsonResult();

        Integer collectCount = travelService.queryCollectCount(travelId);
        if (collectCount == null){
            collectCount = 0;
        }

        MyCollection myCollection = myCollectionService.queryCollection(travelId, userId);
        if (myCollection==null){
            MyCollection myCollection1 = new MyCollection();
            User currentUser = UserContext.getCurrentUser();
            Travel travel = travelService.queryByTravelId(travelId);
            myCollection1.setTravel(travel);
            myCollection1.setUser(currentUser);
            myCollection1.setCollectTime(new Date());
            myCollectionService.collectTravel(myCollection1);
            travelService.updateCollectCount(collectCount+1,travelId);
            return jsonResult;
        } else {
            myCollectionService.deleteTravel(travelId,userId);
            travelService.updateCollectCount(collectCount-1,travelId);
            return jsonResult.mark(new String(""));
        }
    }

    /**
     * 查询收藏游记
     * 有则显示红色,没有则白色
     */
    @GetMapping
    public JsonResult checkCollection(Long travelId, Long userId){
        JsonResult jsonResult = new JsonResult();
        MyCollection myCollection = myCollectionService.queryCollection(travelId, userId);
        if (myCollection!=null){
            return jsonResult;
        }else {
            return jsonResult.mark(new String(""));
        }

    }

    /**
     * 点击收藏大攻略
     *
     */
    @PostMapping("/strategy")
    public JsonResult collectStrategy(Long strategyId,Long userId){

        JsonResult jsonResult = new JsonResult();

        Integer collectCount = strategyService.queryCollectCount(strategyId);
        if (collectCount == null){
            collectCount = 0;
        }
        MyCollection myCollection = myCollectionService.queryStrategyCollection(strategyId, userId);
        if (myCollection==null){
            MyCollection myCollection1 = new MyCollection();
            User currentUser = UserContext.getCurrentUser();
            Strategy strategy = strategyService.getById(strategyId);
            myCollection1.setStrategy(strategy);
            myCollection1.setUser(currentUser);
            myCollection1.setCollectTime(new Date());
            myCollectionService.collectStrategy(myCollection1);
            strategyService.updateCollectCount(collectCount+1,strategyId);
            return jsonResult;
        } else {
            myCollectionService.deleteStrategy(strategyId,userId);
            strategyService.updateCollectCount(collectCount-1,strategyId);
            return jsonResult.mark(new String(""));
        }
    }
    /**
     * 查询收藏攻略
     * 有则显示红色,没有则白色
     */
    @GetMapping("/strategy")
    public JsonResult checkStrategyCollection(Long strategyId, Long userId){
        JsonResult jsonResult = new JsonResult();
        MyCollection myCollection = myCollectionService.queryStrategyCollection(strategyId,userId);
        if (myCollection!=null){
            return jsonResult;
        }else {
            return jsonResult.mark(new String(""));
        }
    }



    /**
     * 点击收藏日报
     *
     */
    @PostMapping("/news")
    public JsonResult collectNews(Long newsId,  Long userId){

        JsonResult jsonResult = new JsonResult();

        Integer collectCount = newsService.queryCollectCount(newsId);
        if (collectCount == null){
            collectCount = 0;
        }
        MyCollection myCollection = myCollectionService.queryNewsCollection(newsId, userId);
        if (myCollection==null){
            MyCollection myCollection1 = new MyCollection();
            User currentUser = UserContext.getCurrentUser();
            News news = newsService.getById(newsId);
            myCollection1.setNews(news);
            myCollection1.setUser(currentUser);
            myCollection1.setCollectTime(new Date());
            myCollectionService.collectNews(myCollection1);
            newsService.updateCollectCount(collectCount+1,newsId);
            return jsonResult;
        } else {
            myCollectionService.deleteNews(newsId,userId);
            newsService.updateCollectCount(collectCount-1,newsId);
            return jsonResult.mark(new String(""));
        }
    }
    /**
     * 查询日报攻略
     * 有则显示红色,没有则白色
     */
    @GetMapping("/news")
    public JsonResult checkNewsCollection(Long newsId, Long userId){
        JsonResult jsonResult = new JsonResult();
        MyCollection myCollection = myCollectionService.queryNewsCollection(newsId,userId);
        if (myCollection!=null){
            return jsonResult;
        }else {
            return jsonResult.mark(new String(""));
        }

    }
}
